package com.msh.artascope.devcloud.service.client;

import com.msh.artascope.devcloud.client.po.GitlabConfigPo;
import com.msh.frame.client.exception.BizException;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.models.GitlabCommit;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabProjectMember;
import org.gitlab.api.query.ProjectsQuery;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Slf4j
@Component
@AllArgsConstructor
public class GitlabClient {

  public Long getProjectId(GitlabConfigPo gitlabConfigPo, String gitUrl){
    try {
      String targetData = gitUrl.split(":")[1];
      String nameWithNamespace = targetData.substring(0,targetData.lastIndexOf("."));
      String gitProjectName = nameWithNamespace.substring(nameWithNamespace.lastIndexOf("/")+1, nameWithNamespace.length());
      ProjectsQuery projectsQuery = new ProjectsQuery();
      projectsQuery.setSearch(gitProjectName);
      // 获取任务列表
      List<GitlabProject> projects = newGitlabAPI(gitlabConfigPo).getProjects(projectsQuery);
      if(CollectionUtils.isEmpty(projects)){
        return null;
      }
      GitlabProject gitlabProject = projects.stream().filter(p -> gitUrl.equals(p.getSshUrl()))
          .findAny().orElse(null);
      if(null == gitlabProject){
        return null;
      }
      return gitlabProject.getId().longValue();
    } catch (Exception e) {
      log.error("GitlabWrapper getProjectId error", e);
      throw BizException.build("获取git项目id失败");
    }
  }

  public String getLastCommitId(GitlabConfigPo gitlabConfigPo, Long projectId, String branch) {
    try {
      List<GitlabCommit> lastCommits = newGitlabAPI(gitlabConfigPo).getLastCommits(projectId.intValue(), branch);
      if(CollectionUtils.isEmpty(lastCommits)){
        return null;
      }
      return lastCommits.get(0).getId();
    }catch (Exception e){
      log.error("GitlabWrapper getCommit error", e);
      throw BizException.build("获取提交记录失败");
    }
  }


  /**
   * 获取会员权限等级
   * @param projectId
   * @param username
   * @return
   *  Guest Reporter Developer Master Owner
   *
   */
  public String memberAccessLevel(GitlabConfigPo gitlabConfigPo, Long projectId, String username){
    try {
      List<GitlabProjectMember> projectMembers = newGitlabAPI(gitlabConfigPo).getProjectMembers(projectId);
      if(CollectionUtils.isEmpty(projectMembers)){
        return null;
      }
      GitlabProjectMember gitlabProjectMember = projectMembers.stream()
          .filter(p -> p.getUsername().equals(username)).findAny().orElse(null);
      if(null == gitlabProjectMember){
        return null;
      }
      return gitlabProjectMember.getAccessLevel().toString();
    }catch (Exception e){
      log.error("GitlabWrapper memberAccessLevel error", e);
      throw BizException.build("获取用户项目权限失败");
    }
  }

  private GitlabAPI newGitlabAPI(GitlabConfigPo gitlabConfigPo){
    return GitlabAPI.connect(gitlabConfigPo.getAddress(), gitlabConfigPo.getToken());
  }
}
